package file.day9_23;

public class SyncDemo2 {
    public static void main(String[] args) {
        Shop shop = new Shop();
        Thread t1 = new Thread(){
            public void run(){
                shop.buy();
            }
        };

        Thread t2 = new Thread(){
            public void run(){
                shop.buy();
            }
        };

        t1.start();
        t2.start();

    }
}
class Shop{
    public void buy(){
        Thread t = Thread.currentThread();

        try {
            System.out.println(t.getName() + "正在挑选衣服");
            Thread.sleep(5000);
            synchronized (this){//this指的就是shop对象;
                //引用对象都可以使用，但是一般是this，因为this是动态的指向；
                //使用synchronized主要是解决抢问题，才排队，且在排队的线程执行顺序是随机的
                System.out.println(t.getName() + "正是试衣服");
                Thread.sleep(5000);

            }
            System.out.println(t.getName() + "结账离开");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
